var app = new Vue({
  el: "#app",
  data: {
    cellStyle: {
      color: "#333",
      textAlign: "center",
      position: "relative",
    },
    headerCellStyle: {
      color: "#999",
      textAlign: "center",
      fontWeight: "bold",
    },
    title: "Hello!",
    // activeIndex: "0",    // 下拉菜单下标
    prjTitle: null,         // 项目名称
    prjPath: null,          // 项目请求路径
    prjName: null,          // 项目请求api
    prjList: [],            // 项目列表
    tableData: [],          // 版本列表
    arrIndex: 0,            // 所选项目下标
    loading: false,         // loading flag
    lastVersion: null,      // 最新版本号
    isMobile: false,        // 是否是移动端
    dialogFlag: false,      // 弹框
    inputTitle: "",         // 新建项目名
    inputPath: "",          // 新建项目文件夹名
    addFlag: false,         // 新建按钮状态
    qr: false,              // qr
    lock: true,            // 权限
  },
  computed: {
    formartSize() {
      return (num) => {
        if (num == 0) return "0 B";
        let k = 1024;
        let sizeStr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; //容量单位
        let i = 0;
        for (let l = 0; l < 8; l++) {
          if (num / Math.pow(k, l) < 1) {
            break; //小于1跳出循环
          }
          i = l; //不小于1的话这个单位就合适或者还要大于这个单位 接着循环
        }
        return (num / Math.pow(k, i)).toFixed(3) + " " + sizeStr[i];
      };
    },
  },
  created() {
    // this.init();
  },
  methods: {
    async init() {
      let ret = await myPost("/upload/getall", { id: 1 });
      console.log(ret.data);
      this.prjList = ret.data.arr;
    },

    async handleSelect(key, keyPath) {
      let col = keyPath[0];
      let row = keyPath[1];
      // console.log(col);
      // console.log(row);
      // 新建
      if (row === "add") {
        return (this.dialogFlag = true);
      }
      // 跳出
      if (col != 2) {
        this.prjName = null;
        this.prjTitle = null;
        return;
      }

      this.arrIndex = row;
      console.log("第" + col + "列的第" + row + "个");

      this.loading = true;
      await this.init();
      await this.handleTable();
      this.loading = false;
    },

    // tableData
    async handleTable() {
      // this.loading = true

      let data = this.prjList[this.arrIndex];
      this.tableData = data.versions;
      // 最新版本
      if (this.tableData.length > 0) {
        this.lastVersion = this.tableData[0].originalname;
      } else {
        this.lastVersion = null;
      }
      // title and api-path
      this.prjTitle = data.name;
      this.prjName = data.dirName;
      this.prjPath = "/upload/" + data.dirName;

      let ret = await myPost("/upload/changeprj", { data });
      console.log("change prj success");
      // this.loading = false
      return ret.versions;
    },

    async refInit() {
      this.loading = true;

      await this.init();
      let data = this.prjList[this.arrIndex];
      this.tableData = data.versions;
      // 最新版本
      if (this.tableData.length > 0) {
        this.lastVersion = this.tableData[0].originalname;
      } else {
        this.lastVersion = null;
      }

      // await this.handleTable(1)

      this.loading = false;
    },

    // upload
    async handleUpload(res, file, list) {
      //  console.log("upload success");
      //  console.log(res);
      console.log(file);
      // console.log(list);
      this.$notify({
        title: "上传成功",
        message: "文件名:" + file.name,
        type: "success",
      });

      this.refInit();
    },
    handleProgerss(event, file) {
      // console.log('event .........');
      console.log(event.percent);
    },
    checkUpload(file) {
      console.log("上传之前");
      let currentVer = file.name.replace(".apk", "");

      // 正则验证
      let reg = /^([1-9]\d|[1-9])(\.([1-9]\d|\d)){2}$/;
      let r = currentVer.match(reg);
      console.log("正则结果:");
      console.log(r);
      if (r == null) {
        this.$notify({
          title: "警告",
          message: "名称不合法! 例: 1.0.0.apk",
          type: "warning",
        });
        return false;
      }

      if (this.lastVersion == null) return true;

      if (currentVer <= this.lastVersion) {
        this.$notify({
          title: "警告",
          message: "上传版本必须大于当前最新版本",
          type: "warning",
        });
        return false;
      }
      return currentVer;
    },

    // reset
    async resetInfo() {
      console.log("开始初始化");
      let data = this.prjList[this.arrIndex];
      let ret = await myPost("/upload/reset-info", { data });
      console.log("ret:");
      console.log(ret.data);
      this.$notify({
        title: "初始化成功",
        type: "success",
      });
      this.refInit();
    },

    // 新建项目 判断驶入是否有空
    addChange() {
      let title = this.inputTitle;
      let path = this.inputPath;

      if (title == "" || path == "") {
        console.log('有空值');
        this.addFlag = false
      }else{
        this.addFlag = true
      }
    },
    // 新建提交
    async checkPath() {
      let title = this.inputTitle;
      let path = this.inputPath;
      // 正则-特殊字符
      let reg2 = /^[\u9fa5a-z]+$/gi;//只能输入英文字母
      let regx = reg2.test(path)

      if (!regx) {
        this.$notify.error({
          title: '错误',
          message: '项目路径必须是纯英文!'
        });
        return
      }

      // 
      let data = {title,path}
      let ret = await myPost("/upload/create-prj", { data });
      if (ret.data.data != 1) {
        this.$notify.error({
          title: '错误',
          message: ret.data.msg
        });
        return
      }
      this.$notify({
        title: '成功',
        message: '新建项目成功',
        type: 'success'
      });
      console.log(window.location.reload());
    },

    // qr
    mouseIn(row, column, cell, event) {
      if (column.label != "二维码") return;
      console.log(column);
      this.qr = true;
      this.$nextTick(() => {
        // console.log(vm.$el.innerText)
        this.qrcode(row);
      });
    },
    mouseOut(row, column, cell, event) {
      if (column.label != "二维码") return;
      this.qr = false;
    },
    qrcode(row) {
      let url = window.location.href
      console.log(url);
      let qrcode = new QRCode("qrcode", {
        width: 200,
        height: 200,
        text: url + row.path, // 二维码地址
      });
    },

    // lock
    unlock() {
      if (!this.lock) return
      let _this = this
      this.$prompt('请输入密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'password',
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
        inputErrorMessage: '不能为空'
      }).then(({ value }) => {
        _this.handleLock(value)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });       
      });
    },
    async handleLock(psd) {
      // console.log('psd: ' + psd);
      let ret = await myPost("/upload/check-lock", { psd });
      
      this.lock = ret.data.lock
      if (!this.lock) {
        this.$notify({
          title: '成功',
          message: '解锁成功',
          type: 'success'
        });
      }else{
        this.$notify.error({
          title: '错误',
          message: '密码错误!!'
        });
      }
    }
  },
  mounted() {
    this.isMobile = mobileCheck();

    this.init();
  },
});
